/*
 * 显示招标公告中的服务器类的相关信息
 */
package cn.com.truelore;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.util.List;

import org.xml.sax.InputSource;

import android.text.Html;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;
import cn.com.truelore.adapter.ZhaoBiaoGGListAdapter;
import cn.com.truelore.base.BaseActivity;
import cn.com.truelore.parser.ZhaoBiaoGGDetailSaxParseHtml;
import cn.com.truelore.parser.ZhaoBiaoGGSaxParseHtml;
import cn.com.truelore.vo.ZhaoBiaoGGVO;

/**
 * 程序的主界面
 * 
 * @author Administrator
 * 
 */
public class ZhaoBiao_FuWuActivity extends BaseActivity {

	List<ZhaoBiaoGGVO> listPageVOList = null;
	ListView listViewPriceView1 = null;
	ZhaoBiaoGGListAdapter _adapter = null;
	ViewFlipper viewFlipperPrice = null;
	TextView textViewDetail = null;

	public ZhaoBiao_FuWuActivity() {
		super(R.layout.tab_zhaobiaogg_list);
	}

	public void initPageData() {
		super.initPageData();
		viewFlipperPrice = (ViewFlipper) this
				.findViewById(R.id.viewFlipperPrice);
		listViewPriceView1 = (ListView) findViewById(R.id.listViewPriceView1);
		textViewDetail = (TextView) findViewById(R.id.textViewGGDetail);
		_adapter = new ZhaoBiaoGGListAdapter(this);
		// list view data
		initListView();
	}

	/**
	 * 初始化ListView1
	 */
	private void initListView() {
		try {
			String strServiceUrl = "http://www1.njcein.com.cn/Njxx/";
			URI uri = new URI(strServiceUrl);
			URL url = uri.toURL();
			HttpURLConnection httpUrlConnection = (HttpURLConnection) url
					.openConnection();

			// 设置通用的请求属性
			httpUrlConnection.setRequestProperty("accept", "*/*");
			httpUrlConnection.setRequestProperty("User-Agent",
					"Chinaunicom /1.0 (Wlan Client;Android 1.6)");
			httpUrlConnection.setRequestProperty("Content-Type",
					"application/xml");
			httpUrlConnection.setRequestProperty("Accept-Encoding", "deflate");
			httpUrlConnection.setRequestProperty("connection", "Keep-Alive");
			httpUrlConnection.setRequestProperty("Cache-Control", "no-cache");
			// httpUrlConnection.setRequestProperty("aaa", "xxxx");

			// 可读取
			httpUrlConnection.setDoInput(true);
			httpUrlConnection.setDoOutput(true);
			httpUrlConnection.setRequestMethod("GET");

			// set request header
			httpUrlConnection.setRequestProperty("SOAPAction", "");
			// httpUrlConnection.setRequestProperty("Content-Length",
			// soapReuqest.length() + "");

			// fStream.close();

			StringBuilder sb = new StringBuilder();
			int intResponseCode = httpUrlConnection.getResponseCode();

			// http status ok
			if (HttpURLConnection.HTTP_OK == intResponseCode) {

				InputStream stream = httpUrlConnection.getInputStream();

				InputStreamReader inputStreamReader = new InputStreamReader(
						stream, "GBK");

				InputSource inputSource = new InputSource(inputStreamReader);

				ZhaoBiaoGGSaxParseHtml parser = new ZhaoBiaoGGSaxParseHtml();// 实例化Parse
				// XMLWriter writer = new XMLWriter();// 实例化XMLWriter，即SAX内容处理器
				// parser.setContentHandler(writer);// 设置内容处理器

				parser.parse(inputSource);

				// 遍历结果
				listPageVOList = parser.getListZhaoBiaoGG();
				stream.close();
			}
			httpUrlConnection.disconnect();
			if (listPageVOList != null) {
				_adapter.setDataList(listPageVOList);
				listViewPriceView1.setAdapter(_adapter);
				listViewPriceView1.setItemsCanFocus(false);
				listViewPriceView1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
				listViewPriceView1
						.setOnItemClickListener(new OnItemClickListener() {
							public void onItemClick(AdapterView<?> parent,
									View view, int position, long id) {
								// 触发事件处理
								listViewPriceView1_onItemClick(parent, view,
										position, id);

							}
						});
			}

		} catch (Exception ex) {
			Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
		}
	}

	/**
	 * 点击事件
	 */
	private void listViewPriceView1_onItemClick(AdapterView<?> parent,
			View view, int position, long id) {
		try {
			if (listPageVOList != null && listPageVOList.size() >= position) {
				ZhaoBiaoGGVO itemVO = listPageVOList.get(position);

				String strServiceUrl = "http://www1.njcein.com.cn"
						+ itemVO.getLink();
				URI uri = new URI(strServiceUrl);
				URL url = uri.toURL();
				HttpURLConnection httpUrlConnection = (HttpURLConnection) url
						.openConnection();

				// 设置通用的请求属性
				httpUrlConnection.setRequestProperty("accept", "*/*");
				httpUrlConnection.setRequestProperty("User-Agent",
						"Chinaunicom /1.0 (Wlan Client;Android 1.6)");
				httpUrlConnection.setRequestProperty("Content-Type",
						"application/xml");
				httpUrlConnection.setRequestProperty("Accept-Encoding",
						"deflate");
				httpUrlConnection
						.setRequestProperty("connection", "Keep-Alive");
				httpUrlConnection.setRequestProperty("Cache-Control",
						"no-cache");
				// httpUrlConnection.setRequestProperty("aaa", "xxxx");

				// 可读取
				httpUrlConnection.setDoInput(true);
				httpUrlConnection.setDoOutput(true);

				httpUrlConnection.setRequestMethod("GET");

				// set request header
				httpUrlConnection.setRequestProperty("SOAPAction", "");
				// httpUrlConnection.setRequestProperty("Content-Length",
				// soapReuqest.length() + "");

				// fStream.close();

				StringBuilder sb = new StringBuilder();
				int intResponseCode = httpUrlConnection.getResponseCode();
				String detailValue = "";

				// http status ok
				if (HttpURLConnection.HTTP_OK == intResponseCode) {

					InputStream stream = httpUrlConnection.getInputStream();

					InputStreamReader inputStreamReader = new InputStreamReader(
							stream, "GBK");

					InputSource inputSource = new InputSource(inputStreamReader);

					ZhaoBiaoGGDetailSaxParseHtml parser = new ZhaoBiaoGGDetailSaxParseHtml();// 实例化Parse
					// XMLWriter writer = new XMLWriter();//
					// 实例化XMLWriter，即SAX内容处理器
					// parser.setContentHandler(writer);// 设置内容处理器

					parser.parse(inputSource);

					detailValue = parser.getDetailContext();

					stream.close();
				}
				httpUrlConnection.disconnect();

				/*
				 * textViewTitle.setText(strType + " " + strCommdType + " " +
				 * priceVO.getENm());
				 */
				if (detailValue != null) {
					textViewDetail.setText(Html.fromHtml(detailValue));
					viewFlipperPrice.showNext();
				}
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
